package com.qiguliuxing.dts.merchant.service;

import com.google.common.collect.Lists;
import com.qiguliuxing.dts.db.bean.DtsMerchantApplyInfo;
import com.qiguliuxing.dts.db.dao.DtsMerchantApplyMapper;
import com.qiguliuxing.dts.db.dao.DtsSystemMapper;
import com.qiguliuxing.dts.db.domain.*;
import com.qiguliuxing.dts.service.base.DtsCategoryService;
import com.qiguliuxing.dts.service.system.SystemConfig;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 商户入驻申请服务类
 * @author qiguliuxing
 * @since 1.0.0
 */
@Service
public class MerchantApplyService {

    @Resource
    private DtsMerchantApplyMapper merchantApplyMapper;

    @Resource
    private DtsCategoryService categoryService;

    @Resource
    private MerchantAdminService merchantAdminService;

    @Resource
    private DtsSystemMapper dtsSystemMapper;



    public Integer addMerchantApply(DtsMerchantApplyInfo merchantApplyInfo) {
        DtsMerchantApply dtsMerchantApply = new DtsMerchantApply();
        DtsAdmin dtsAdmin = merchantAdminService.findAllMerchantAdmin(merchantApplyInfo.getUsername());
        dtsMerchantApply.setAdminId(dtsAdmin.getId());
        dtsMerchantApply.setShopName(merchantApplyInfo.getShopName());
        dtsMerchantApply.setCategoryId(merchantApplyInfo.getMainManage());
        dtsMerchantApply.setIdHeadPic(merchantApplyInfo.getIdHeadPic());
        dtsMerchantApply.setIdNationalEmblemPic(merchantApplyInfo.getIdNationalEmblemPic());
        dtsMerchantApply.setIsThreeSyndromesInOne(merchantApplyInfo.getIsThreeCertInOne());
        dtsMerchantApply.setBusinessLicensePic(merchantApplyInfo.getBusinessLicense());
        dtsMerchantApply.setCreateTime(LocalDateTime.now());
        dtsMerchantApply.setUpdateTime(LocalDateTime.now());
        return merchantApplyMapper.insertSelective(dtsMerchantApply);
    }

    public List<DtsCategory> queryAllMerchantCategory(){
        return categoryService.allCategory();
    }

    public void updateMerchantApply(DtsMerchantApplyInfo merchantApplyInfo,Integer adminId) {
        DtsMerchantApplyExample example = new DtsMerchantApplyExample();
        DtsMerchantApplyExample.Criteria criteria = example.createCriteria();
        criteria.andAdminIdEqualTo(adminId);
        DtsMerchantApply dtsMerchantApply = merchantApplyMapper.selectOneByExample(example);
        dtsMerchantApply.setShopName(merchantApplyInfo.getShopName());
        dtsMerchantApply.setCategoryId(merchantApplyInfo.getMainManage());
        dtsMerchantApply.setIdHeadPic(merchantApplyInfo.getIdHeadPic());
        dtsMerchantApply.setIdNationalEmblemPic(merchantApplyInfo.getIdNationalEmblemPic());
        dtsMerchantApply.setIsThreeSyndromesInOne(merchantApplyInfo.getIsThreeCertInOne());
        dtsMerchantApply.setBusinessLicensePic(merchantApplyInfo.getBusinessLicense());
        dtsMerchantApply.setUpdateTime(LocalDateTime.now());
        merchantApplyMapper.updateByPrimaryKeySelective(dtsMerchantApply);
    }

    public List<String> applyConfig() {
        return Lists.newArrayList(SystemConfig.getExampleLicensePic(),SystemConfig.getPlatformProtocol());
    }

}
